﻿using System;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using CarlosAg.ExcelXmlWriter;
using System.Net.Mail;
using System.Text;
using eCommerce.DataAccess;

namespace eCommerce.CMS.CustomAdmin.Controls.Subscribers
{
    public partial class List : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                WebsiteDataContext db = new WebsiteDataContext();
                var newsletter = db.getNewsletter();
                GridView1.DataSource = newsletter;
                GridView1.DataBind();

            }
        }
        protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                //e.Row.Cells[1].Visible = false;
                //e.Row.Cells[2].Visible = false;
            }

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //e.Row.Cells[1].Visible = false;
                //e.Row.Cells[2].Visible = false;
                ImageButton MinButton = (ImageButton)e.Row.Cells[0].FindControl("MinBT");
                MinButton.CommandArgument = e.Row.RowIndex.ToString();
                ImageButton addButton = (ImageButton)e.Row.Cells[0].FindControl("PluseBT");
                addButton.CommandArgument = e.Row.RowIndex.ToString();
                //e.Row.Cells[3].Visible = false;
            }
        }
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string ShowHide = e.Row.Cells[2].Text;
                //ShowHide = ShowHide.Replace("0", "");
                if (ShowHide == "0")
                {
                    ImageButton Bt_Min = (ImageButton)e.Row.Cells[0].FindControl("MinBT");
                    Bt_Min.Visible = true;
                    ImageButton Bt_plus = (ImageButton)e.Row.Cells[0].FindControl("PluseBT");
                    Bt_plus.Visible = false;
                }
                else
                {
                    ImageButton Bt_Min = (ImageButton)e.Row.Cells[0].FindControl("MinBT");
                    Bt_Min.Visible = false;
                    ImageButton Bt_plus = (ImageButton)e.Row.Cells[0].FindControl("PluseBT");
                    Bt_plus.Visible = false;
                }
            }
        }
        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {

            if (e.CommandName == "_Show")
            {
                int index = Convert.ToInt32(e.CommandArgument);
                GridViewRow row = GridView1.Rows[index];
                int G_Count = GridView1.Rows.Count;
                for (int i = index + 1; i < G_Count; i++)
                {
                    if (GridView1.Rows[i].Cells[2].Text == "0")
                    {
                        ImageButton Bt_Min = (ImageButton)row.Cells[0].FindControl("MinBT");
                        Bt_Min.Visible = true;
                        ImageButton Bt_plus = (ImageButton)row.Cells[0].FindControl("PluseBT");
                        Bt_plus.Visible = false;
                        break;
                    }
                    else
                    {
                        GridView1.Rows[i].Visible = true;


                    }
                    ImageButton Bt_Min1 = (ImageButton)row.Cells[0].FindControl("MinBT");
                    Bt_Min1.Visible = true;
                    ImageButton Bt_plus1 = (ImageButton)row.Cells[0].FindControl("PluseBT");
                    Bt_plus1.Visible = false;
                }

            }
            if (e.CommandName == "_Hide")
            {
                int index = Convert.ToInt32(e.CommandArgument);
                GridViewRow row = GridView1.Rows[index];
                int G_Count = GridView1.Rows.Count;
                for (int i = index + 1; i < G_Count; i++)
                {
                    if (GridView1.Rows[i].Cells[2].Text == "0")
                    {
                        ImageButton Bt_Min = (ImageButton)row.Cells[0].FindControl("MinBT");
                        Bt_Min.Visible = false;
                        ImageButton Bt_plus = (ImageButton)row.Cells[0].FindControl("PluseBT");
                        Bt_plus.Visible = true;
                        break;
                    }
                    else
                    {
                        GridView1.Rows[i].Visible = false;


                    }
                    ImageButton Bt_Min1 = (ImageButton)row.Cells[0].FindControl("MinBT");
                    Bt_Min1.Visible = false;
                    ImageButton Bt_plus1 = (ImageButton)row.Cells[0].FindControl("PluseBT");
                    Bt_plus1.Visible = true;
                }


            }
        }
        protected void btn_Backup_Onclick(object sender, EventArgs e)
        {
            var sConnection = ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString;
            const string sCommnadEmails = "SELECT [name] ,[email] ,[status] FROM  [custom_subscriber]";
            Workbook bookEml = new Workbook();
            var sheetEml = bookEml.Worksheets.Add("subscribers");
            SqlDataAdapter sqldtAdpEml = new SqlDataAdapter(sCommnadEmails, sConnection);
            DataSet dtStEml = new DataSet();
            sqldtAdpEml.Fill(dtStEml);

            for (int i = 0; i < dtStEml.Tables[0].Rows.Count; i++)
            {
                var rowEml = sheetEml.Table.Rows.Add();

                for (int j = 0; j < dtStEml.Tables[0].Columns.Count; j++)
                {
                    rowEml.Cells.Add(dtStEml.Tables[0].Rows[i][j].ToString());
                }
            }
            var sDate = DateTime.Now.ToShortDateString();
            sDate = sDate.Replace("/", "");
            var sPathEml = HttpContext.Current.Server.MapPath("backup") + "/subscribers_" + sDate + ".xls";
            bookEml.Save(sPathEml);


            MailAddress from = new MailAddress("subscribers@cms.2beknown.co.uk");
            //MailAddress to = new MailAddress("info@example.com");
            MailAddress to = new MailAddress("tavis@2beknown.co.uk");
            MailMessage message = new MailMessage(from, to);

            message.Subject = "Subscribers Backup";
            StringBuilder sb = new StringBuilder();
            sb.Append("<html><body style=\"height:auto;margin:0 auto;\"><div style=\"font-family:Arial;font-size:11px;float:left;height:auto;margin:0 auto;color:#3F3F3F;padding:10px;\">");
            sb.Append(" <img src=\"http://www.example.com/App_Themes/default/graphics/logosmall.jpg\" alt=\"Example\" /><br /><br />");
            sb.Append("<b>Latest Subscribers</b><br />");

            sb.Append("</div></body></html>");
            message.Body = sb.ToString();
            message.IsBodyHtml = true;
            message.Attachments.Add(new Attachment(sPathEml));

            SmtpClient smtp = new SmtpClient("localhost", 25);
            smtp.Send(message);



        }

        protected void ButtonDeleteCommand(object sender, CommandEventArgs e)
        {
            Response.Redirect("subscriber.aspx?action=Delete&PageID=" + e.CommandArgument);
        }

        protected void ButtonEditCommand(object sender, CommandEventArgs e)
        {
            Response.Redirect("subscriber.aspx?action=Edit&PageID=" + e.CommandArgument);
        }
    }
}